package cn.meibang.common.beauty.provider.service;

import cn.apestar.core.utils.DateUtils;
import cn.meibang.common.beauty.api.IBcoUserOrderAPIService;
import cn.meibang.common.beauty.provider.dao.BeautyConsultationOrderUserMapper;
import cn.meibang.common.beauty.provider.response.BcoUserCountStatsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by admin on 2017-6-28.
 */
@Service
public class BcoUserOrderServiceProvider implements IBcoUserOrderAPIService {

    @Autowired
    private BeautyConsultationOrderUserMapper bcoUserMapper;

    @Override
    public BcoUserCountStatsResponse countOrderUser(Long consultantId) {
        BcoUserCountStatsResponse res = new BcoUserCountStatsResponse();
        Date today = DateUtils.now();
        Date yesterday = DateUtils.addDay(today, -1);
        Date beforeYesterday = DateUtils.addDay(today, -2);
        res.setConsultantId(consultantId);
        res.setTodayTotalCount(countTotal(today, consultantId));
        res.setTodayDealCount(countDeal(today, consultantId));
        res.setYesterdayTotalCount(countTotal(yesterday, consultantId));
        res.setYesterdayDealCount(countDeal(yesterday, consultantId));
        res.setTheDayBeforeYesterdayTotalCount(countTotal(beforeYesterday, consultantId));
        res.setTheDayBeforeYesterdayDealCount(countDeal(beforeYesterday, consultantId));
        return res;
    }

    private int countTotal(Date date, Long userId) {
        Date dateStart = DateUtils.getOneDayStartTime(date);
        Date dateEnd = DateUtils.getOneDayEndTime(date);
        return bcoUserMapper.countTotal(dateStart, dateEnd, userId);
    }

    private int countDeal(Date date, Long userId) {
        Date dateStart = DateUtils.getOneDayStartTime(date);
        Date dateEnd = DateUtils.getOneDayEndTime(date);
        return bcoUserMapper.countDeal(dateStart, dateEnd, userId);
    }
}
